package com.yuexiao.controller;

import com.yuexiao.entity.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName:GlobalExceptionHandler
 * @Auther: YUEXIAO
 * @Description: 全局异常处理
 * @Date: 2022/8/11 14:04
 * @Version: v1.0
 */
@ControllerAdvice(annotations = {RestController.class,Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    /*
        异常处理方法
     */
    @ExceptionHandler(Exception.class)
    public ResultInfo exceptionInfo(Exception ex){
        log.error("拦截错误,{},结束",ex.getMessage());
        if(ex.getMessage().contains("未登录")){
            return ResultInfo.error("用户未登录");
        }
        return ResultInfo.error("未知错误");
    }

}
